package com.zhtools.excel.tester;

import java.math.BigDecimal;
import java.util.Random;

public class OrderItemFactory {
    private static final char[] ARRAYS = new char[]{
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
            'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
            'u', 'v', 'w', 'x', 'y', 'z'
    };

    public static String randomString(int length) {
        int size = ARRAYS.length;

        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            buffer.append(ARRAYS[random.nextInt(size)]);
        }
        return buffer.toString();
    }

    public static OrderItem createOrderItem() {
        long pId = (long) (10 + Math.random() * 100);
        int amount = (int) (1 + Math.random() * 10);
        double price = 1 + Math.random() * 100;

        OrderItem orderItem = new OrderItem();
        orderItem.setpId(pId);
        orderItem.setTitle(randomString(32));
        orderItem.setAmount(amount);
        orderItem.setPrice(new BigDecimal(price).setScale(2, BigDecimal.ROUND_HALF_UP));
        return orderItem;
    }
}
